﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Castle.ActiveRecord;
using NHibernate.Criterion;
using LIB;
using System.Collections;

public partial class admin_nha_dat_tho_cu_Editor : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                using (new SessionScope())
                {
                    bindHuong();
                    bindDacDiemDuong();
                    bindMucGia();
                    bindLoaiBDS();
                    bindQuanHuyen();

                    int id = ConvertUtility.ToInt32(Request.QueryString["id"]);
                    if (id > 0)
                    {
                        hidID.Value = id.ToString();
                        initForm(true);
                    }
                    else
                    {
                        initForm(false);
                    }

                    fail.Visible = false;
                }
            }
        }
        catch (Exception ex) { }
    }
   
    protected void bindHuong()
    {
        ddlHuong.DataSource = Utils.getHuong();
        ddlHuong.DataBind();
    }
    protected void bindDacDiemDuong()
    {
        ddlDDDuong.DataSource = Utils.getDacDiemDuong();
        ddlDDDuong.DataBind();
    }
    protected void bindQuanHuyen()
    {
        ddlQuanHuyen.DataSource = Utils.getQuanHuyen();
        ddlQuanHuyen.DataBind();

        ddlPhuong.DataSource = Utils.getPhuong(ConvertUtility.ToInt32(ddlQuanHuyen.SelectedValue));
        ddlPhuong.DataBind();
    }
    protected void bindMucGia()
    {
        ddlMucGia.DataSource = Utils.getMucGia();
        ddlMucGia.DataBind();
    }
    protected void bindLoaiBDS()
    {
        ddlLoaiBDS.DataSource = Utils.getLoaiBDS();
        ddlLoaiBDS.DataBind();
    }


    protected void initForm(bool isEdit)
    {
        if (isEdit)
        {
            btnAdd.Visible = false;
            btnEdit.Visible = true;
            lblAction.Text = "Sửa bất động sản";
            fillData();
        }
        else
        {
            btnAdd.Visible = true;
            btnEdit.Visible = false;
            lblAction.Text = "Thêm mới bất động sản ";
            string htmlCommon = "";
            htmlCommon += "<div><b>Đặc điểm đường:</b> text</div>";
            htmlCommon += "<div><b>Năm xây dựng:</b> text</div>";
            htmlCommon += "<div><b>Diện tích mặt bằng:</b> text</div>";
            htmlCommon += "<div><b>Hướng:</b> text</div>";
            htmlCommon += "<div><b>Pháp lý:</b> text</div>";
            htmlCommon += "<div><b>Số tầng:</b> text</div>";
            htmlCommon += "<div><b>Mô tả chi tiết:</div>";
            htmlCommon += "<div class=\"chitiet>\"text</div>";
           // radCommonInfo.Value = htmlCommon;

            string htmlPrivate = "";
            htmlPrivate += "<div><b>Địa chỉ:</b> text</div>";
            htmlPrivate += "<div><b>Chủ hộ:</b> text</div>";
            htmlPrivate += "<div><b>Tên:</b> text</div>";
            htmlPrivate += "<div><b>Số điện thoại:</b> text</div>";
            htmlPrivate += "<div><b>Email:</b> text</div>";
            htmlPrivate += "<div><b>Ghi chú:</b>text</div>";
            //fckPrivateInfo.Value = htmlPrivate;
        }
    }


    protected void fillData()
    {
        try
        {
            Home_Info homeInfo = Home_Info.Find(ConvertUtility.ToInt32(hidID.Value));
            imgAvatar.ImageUrl = App_Constant.PATH_UP_IMAGES_HOME + "/" + homeInfo.homeAvatar;
            hidAvatar.Value = homeInfo.homeAvatar;
            txtTen.Text = homeInfo.homeName;
            txtGia.Text = ConvertUtility.ToString(homeInfo.homeGia);
            txtDienTich.Text = ConvertUtility.ToString(homeInfo.homeArea);
            //fckIntro.Value = homeInfo.homeIntro;
            //radCommonInfo.Value = homeInfo.homeComonInfo;
            txtDemoVideo.Text = homeInfo.homeDemoVideo;
            litDemoVideo.Text = homeInfo.homeDemoVideo;

            imgMap.ImageUrl = App_Constant.PATH_UP_IMAGES_MAP + "/" + homeInfo.homeDemoBanDo;
            hidDemoMap.Value = homeInfo.homeDemoBanDo;

            Conjunction conj = new Conjunction();
            conj.Add(Expression.Eq("imageType", 1));
            conj.Add(Expression.Eq("imageObjectID", homeInfo.id));

            Image_Info[] lstImages = Image_Info.FindAll(conj);
            hidImgContent.Value = "";
            if (lstImages.Length > 0)
            {
                for (int i = 0; i < lstImages.Length; i++)
                    hidImgContent.Value += lstImages[i].imageUrl + "|";
            }

            Home_Cat_Info[] lstHomeCat = Home_Cat_Info.FindAll(Expression.Eq("homeID", homeInfo.id));
            hidCatID.Value = "";
            if (lstHomeCat.Length > 0)
            {
                for (int i = 0; i < lstHomeCat.Length; i++)
                    hidCatID.Value += lstHomeCat[i].catID + "|";
            }

            //fckPrivateInfo.Value = homeInfo.homePrivateInfo;
            if (homeInfo.homeIsShowPrivate)
                chkIsPrivate.Checked = true;
            else
                chkIsPrivate.Checked = false;

        }
        catch (Exception ex) { }
    }
    protected Home_Info getHomeInfo(Home_Info homeInfo)
    {
        homeInfo.homeName = txtTen.Text;
        homeInfo.homeAvatar = (fileUploadAvatar.HasFile) ? App_File.tienToFile() + "." + fileUploadAvatar.PostedFile.FileName : hidAvatar.Value;
        homeInfo.homeGia = ConvertUtility.ToDouble(txtGia.Text, true);
        homeInfo.homeArea = ConvertUtility.ToDouble(txtDienTich.Text, true);
        //homeInfo.homeIntro = fckIntro.Value;
        //homeInfo.homeComonInfo = radCommonInfo.Value;
        homeInfo.homeDemoVideo = txtDemoVideo.Text;
        homeInfo.homeDemoBanDo = (fileUploadMap.HasFile) ? App_File.tienToFile() + "." + fileUploadMap.PostedFile.FileName : hidDemoMap.Value;
        //homeInfo.homePrivateInfo = fckPrivateInfo.Value;
        if (chkIsPrivate.Checked)
            homeInfo.homeIsShowPrivate = true;
        else
            homeInfo.homeIsShowPrivate = false;

        return homeInfo;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            Home_Info homeInfo = getHomeInfo(new Home_Info());

            if (fileUploadAvatar.HasFile)
                App_File.uploadFileOnSerVer(fileUploadAvatar, App_Constant.PATH_UP_IMAGES_HOME + "/", 300, 300);
            if (fileUploadMap.HasFile)
                App_File.uploadFileOnSerVer(fileUploadMap, App_Constant.PATH_UP_IMAGES_MAP + "/", 300, 300);

            homeInfo.CreateAndFlush();
            string[] arrImage = hidImgContent.Value.Split('|');
            if (arrImage.Length > 0)
            {
                for (int i = 0; i < arrImage.Length - 1; i++)
                {
                    Image_Info imgInfo = new Image_Info();
                    imgInfo.imageUrl = arrImage[i];
                    imgInfo.imageObjectID = homeInfo.id;
                    imgInfo.imageType = 1;
                    imgInfo.CreateAndFlush();
                }
            }
            string[] arrCat = hidCatID.Value.Split('|');
            if (arrCat.Length > 0)
            {
                for (int i = 0; i < arrCat.Length - 1; i++)
                {
                    Home_Cat_Info homecatInfo = new Home_Cat_Info();
                    homecatInfo.catID = ConvertUtility.ToInt32(arrCat[i]);
                    homecatInfo.homeID = homeInfo.id;
                    homecatInfo.CreateAndFlush();
                }
            }

            Response.Redirect("/admin/bat-dong-san/default.aspx");
        }
        catch (Exception ex)
        {
            fail.Visible = true;
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            Home_Info homeInfo = getHomeInfo(Home_Info.Find(ConvertUtility.ToInt32(hidID.Value)));

            if (fileUploadAvatar.HasFile)
                App_File.uploadFileOnSerVer(fileUploadAvatar, App_Constant.PATH_UP_IMAGES_HOME + "/", 300, 300);
            if (fileUploadMap.HasFile)
                App_File.uploadFileOnSerVer(fileUploadMap, App_Constant.PATH_UP_IMAGES_MAP + "/", 300, 300);

            homeInfo.SaveAndFlush();

            Image_Info.DeleteAll("imageObjectID=" + homeInfo.id + " and imageType=1");
            string[] arrImage = hidImgContent.Value.Split('|');
            if (arrImage.Length > 0)
            {
                for (int i = 0; i < arrImage.Length - 1; i++)
                {
                    Image_Info imgInfo = new Image_Info();
                    imgInfo.imageUrl = arrImage[i];
                    imgInfo.imageObjectID = homeInfo.id;
                    imgInfo.imageType = 1;
                    imgInfo.CreateAndFlush();
                }
            }

            Home_Cat_Info.DeleteAll("homeID=" + homeInfo.id);
            string[] arrCat = hidCatID.Value.Split('|');
            if (arrCat.Length > 0)
            {
                for (int i = 0; i < arrCat.Length - 1; i++)
                {
                    Home_Cat_Info homecatInfo = new Home_Cat_Info();
                    homecatInfo.catID = ConvertUtility.ToInt32(arrCat[i]);
                    homecatInfo.homeID = homeInfo.id;
                    homecatInfo.CreateAndFlush();
                }
            }

            Response.Redirect("/admin/bat-dong-san/default.aspx");
        }
        catch (Exception ex)
        {
            fail.Visible = true;
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        if (hidID.Value != "")
            fillData();
        else
            ControlUtility.EmptyTextBoxes(this);

    }
}